package com.luo.divview4;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

/**
 * 自定义音量View.
 * <p>
 * Created by Luo on 2017/4/27 0027.
 */
public class CustomVolumView extends View {
    // 自定义属性
    private int firstColor;
    private int secondColor;
    private int circleWidth;
    private int dotCount;
    private int splitSize;
    private Bitmap bg;
    // 画笔
    private Paint mPaint;
    // 范围
    private Rect mRect;
    /*当前进度*/
    private int mCurrentCount = 3;

    public CustomVolumView(Context context) {
        this(context, null);
    }

    public CustomVolumView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomVolumView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomVolumView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomVolumView_firstColor:
                    firstColor = a.getColor(attr, Color.GREEN);
                    break;
                case R.styleable.CustomVolumView_secondColor:
                    secondColor = a.getColor(attr, Color.RED);
                    break;
                case R.styleable.CustomVolumView_circleWidth:
                    circleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomVolumView_dotCount:
                    dotCount = a.getInt(attr, 20);
                    break;
                case R.styleable.CustomVolumView_splitSize:
                    splitSize = a.getInt(attr, 20);
                    break;
                case R.styleable.CustomVolumView_bg:
                    bg = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
                    break;
            }
        }
        // 资源回收
        a.recycle();
        mPaint = new Paint();
        mRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStrokeWidth(circleWidth); // 设置圆环的宽度
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 定义线段断电形状为圆头
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        int center = getWidth() / 2; // 获取圆心的x坐标
        int radius = center - circleWidth / 2;// 半径
        /*
         * 画块块去
         */
        drawOval(canvas, center, radius);

        /*
         * 计算内切正方形的位置
         */
        int relRadius = radius - circleWidth / 2;// 获得内圆的半径
        /*
         * 内切正方形的距离顶部 = mCircleWidth + relRadius - √2 / 2
         */
        mRect.left = (int) (relRadius - Math.sqrt(2) * 1.0f / 2 * relRadius) + circleWidth;
        /*
         * 内切正方形的距离左边 = mCircleWidth + relRadius - √2 / 2
         */
        mRect.top = (int) (relRadius - Math.sqrt(2) * 1.0f / 2 * relRadius) + circleWidth;
        mRect.bottom = (int) (mRect.left + Math.sqrt(2) * relRadius);
        mRect.right = (int) (mRect.left + Math.sqrt(2) * relRadius);

        /*
         * 如果图片比较小，那么根据图片的尺寸放置到正中心
         */
        if (bg.getWidth() < Math.sqrt(2) * relRadius) {
            mRect.left = (int) (mRect.left + Math.sqrt(2) * relRadius * 1.0f / 2 - bg.getWidth() * 1.0f / 2);
            mRect.top = (int) (mRect.top + Math.sqrt(2) * relRadius * 1.0f / 2 - bg.getHeight() * 1.0f / 2);
            mRect.right = mRect.left + bg.getWidth();
            mRect.bottom = mRect.top + bg.getHeight();

        }
        // 绘图
        canvas.drawBitmap(bg, null, mRect, mPaint);
    }

    /**
     * 根据参数画出每个小块
     *
     * @param canvas
     * @param centre
     * @param radius
     */
    private void drawOval(Canvas canvas, int centre, int radius) {
        /**
         * 根据需要画的个数以及间隙计算每个块块所占的比例*360
         */
        float itemSize = (360 * 1.0f - dotCount * splitSize) / dotCount;

        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

        mPaint.setColor(firstColor); // 设置圆环的颜色
        for (int i = 0; i < dotCount; i++) {
            canvas.drawArc(oval, i * (itemSize + splitSize), itemSize, false, mPaint); // 根据进度画圆弧
        }

        mPaint.setColor(secondColor); // 设置圆环的颜色
        for (int i = 0; i < mCurrentCount; i++) {
            canvas.drawArc(oval, i * (itemSize + splitSize), itemSize, false, mPaint); // 根据进度画圆弧
        }
    }

    /**
     * 当前数量+1
     */
    public void up() {
        mCurrentCount++;
        // 这里限制一下最大值
        if (mCurrentCount > dotCount) {
            mCurrentCount = dotCount;
        }
        postInvalidate();
    }

    /**
     * 当前数量-1
     */
    public void down() {
        mCurrentCount--;
        // 这里限制一下最小值
        if (mCurrentCount == -1) {
            mCurrentCount = 0;
        }
        postInvalidate();
    }

    // 这两个用来控制音量加减的
    private int firstPosition, secondPosition = -1;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                firstPosition = (int) event.getY();
                // 下滑
                if (secondPosition - firstPosition > 20 && secondPosition != -1) {
                    down();
                } else if (firstPosition - secondPosition > 20 && secondPosition != -1) {
                    up();
                }
                secondPosition = firstPosition;
                break;
        }
        return true;
    }
}
